<?php

use yii\db\Migration;

/**
 * Class m210315_021953_init.
 */
class m210315_021953_init extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        // 管理员表
        $this->createTable('bk_admin', [
            'id' => $this->primaryKey(),
            'username' => $this->string(20)->notNull()->comment('用户名'),
            'password' => $this->string(64)->notNull()->comment('密码'),
            'token' => $this->string(64),
            'token_expire_at' => $this->integer()->defaultValue(0)->comment('过期时间'),
            'last_login_ip' => $this->string(45)->defaultValue('')->comment('上次登录IP'),
            'last_login_at' => $this->integer()->defaultValue(0)->comment('上次登录时间'),
            'status'=>$this->tinyInteger(1)->defaultValue(1)->comment('1.正常 0.冻结'),
            'created_at' => $this->integer()->notNull(),
            'updated_at' => $this->integer()->notNull(),
        ]);
        $this->createIndex('unq-username', 'bk_admin', 'username', true);
        $this->createIndex('unq-token', 'bk_admin', 'token', true);

        # region 测试数据
        $this->insert('bk_admin', [
            'username' => 'admin',
            'password' => password_hash('123456', PASSWORD_DEFAULT),
            'created_at' => time(),
            'updated_at' => time(),
        ]);
        # endregion

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable('bk_admin');

        echo 'Init Be Reverted.' . PHP_EOL;

        return true;
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m210315_021953_init cannot be reverted.\n";

        return false;
    }
    */
}
